﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using Isuka.Iteration;
using Isuka.Linq;
using Isuka.Spiritual.Auxiliaries;

namespace Isuka.Spiritual
{
    public static class RuleExtensions
    {
        public static ValueConvertibleRule<V, T, A> Filter<V, T, A>(this ValueConvertibleRule<V, T, A> rule, Func<V, bool> filter)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule");
            Contract.Requires<ArgumentNullException>(filter != null, "filter");

            return rule[(a, v) =>
            {
                if (!filter(v))
                {
                    throw new Exception();
                }
            }];
        }


        /// <summary>
        /// 1回以上一致
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <param name="inner"></param>
        /// <returns></returns>
        public static RuleHolder<T, A> OneOrMore<T, A>(this IRule<T, A> inner)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(inner != null, "inner");
            return new RuleHolder<T, A>(inner.Rule.OneOrMore());
        }

        /// <summary>
        /// ゼロ回以上一致
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <param name="inner"></param>
        /// <returns></returns>
        public static RuleHolder<T, A> ZeroOrMore<T, A>(this IRule<T, A> inner)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(inner != null, "inner");
            return new RuleHolder<T, A>(inner.Rule.ZeroOrMore());
        }

        /// <summary>
        /// ゼロまたは1回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <param name="inner"></param>
        /// <returns></returns>
        public static RuleHolder<T, A> ZeroOrOne<T, A>(this IRule<T, A> inner)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(inner != null, "inner");
            Contract.Ensures(Contract.Result<RuleHolder<T, A>>() != null);

            return new RuleHolder<T, A>(inner.Rule.ZeroOrOne());
        }

        public static ValueConvertibleRule<V, T, A> ToConvertible<V, T, A>(this IRule<T, A> inner, Func<IEnumerable<T>, V> conv)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires(inner != null);
            Contract.Requires(conv != null);

            if (inner is ValueConvertibleRule<V, T, A>)
            {
                return (ValueConvertibleRule<V, T, A>)inner;
            }
            else
            {
                return new ValueConvertibleRule<V, T, A>(inner, conv);
            }
        }

        public static ValueConvertibleRule<string, char, A> ToConvertible<A>(this IRule<char, A> inner)
            where A : class
        {
            Contract.Requires(inner != null);


            if (inner is ValueConvertibleRule<string, char, A>)
            {
                return (ValueConvertibleRule<string, char, A>)inner;
            }
            else
            {
                return new ValueConvertibleRule<string, char, A>(inner, x => x.Stringize());
            }
        }

        public static ValueConvertibleRule<V, char, A> ToConvertible<V, A>(this IRule<char, A> inner, Func<string, V> conv)
            where A : class
        {
            Contract.Requires(inner != null);
            Contract.Requires(conv != null);

            if (inner is ValueConvertibleRule<V, char, A>)
            {
                return (ValueConvertibleRule<V, char, A>)inner;
            }
            else
            {
                return new ValueConvertibleRule<V, char, A>(inner, x => conv(x.Stringize()));
            }
        }

        public static RuleHolder<T, A> ToRule<T, A>(this RuleHolder<T, A> rule)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule");
            Contract.Ensures(Contract.Result<RuleHolder<T, A>>() != null);

            return rule;
        }

        public static RuleHolder<T, A> ToRuleHolder<T, A>(this IRule<T, A> rule)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule");
            Contract.Ensures(Contract.Result<RuleHolder<T, A>>() != null);

            return rule is RuleHolder<T, A> ? (RuleHolder<T, A>)rule : new RuleHolder<T, A>(rule.Rule);
        }

        public static RuleHolder<T, A> ToRuleHolder<T, A>(this Rules<T, A>.RuleFunc rule)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule");
            Contract.Ensures(Contract.Result<RuleHolder<T, A>>() != null);

            return new RuleHolder<T, A>(rule);
        }

        public static RuleResult<T> Run<T, A>(this IGrammer<T, A> grammer
                                              , IIterable<IBidirectionalIterator<T>> iterable
                                              , A arg)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(grammer != null, "grammer");
            Contract.Requires<ArgumentNullException>(iterable != null, "iterable");

            var ctx = new RuleContext<T, A>(iterable.Begin(), iterable.End(), arg);
            return grammer.Run(ctx.First, ctx);
        }

        public static RuleResult<char> Run<A>(this IGrammer<char, A> grammer, string source, A arg)
            where A : class
        {
            Contract.Requires(grammer != null);
            Contract.Requires(source != null);

            return grammer.Run(source.AsIterable(), arg);
        }


        public static RuleResult<T> Run<T, A>(this IRule<T, A> rule
                                             , IIterable<IBidirectionalIterator<T>> iterable
                                             , A arg
                                             , Rules<T, A>.SkipFunc skip = null)
            where T : IEquatable<T>
            where A : class
        {
            Contract.Requires<ArgumentNullException>(rule != null, "rule");
            Contract.Requires<ArgumentNullException>(iterable != null, "iterable");

            var ctx = new RuleContext<T, A>(iterable.Begin(), iterable.End(), arg);
            return rule.Rule(ctx.First, ctx, skip ?? Rules<T, A>.NoSkip);
        }

    }
}
